<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeDidlog">
    <!-- 表单 -->
    <el-form ref="formData" label-width="120px" :rules="rules" :model="employeeInfo">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employeeInfo.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="employeeInfo.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="employeeInfo.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employeeInfo.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employmentConst.formOfEmployment" :key="item.id" :value="item.id" :label="item.val" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employeeInfo.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="employeeInfo.departmentName" style="width:80%" placeholder="请选择部门" @focus="isShowTree = true" />
        <el-tree v-if="isShowTree" class="deptsTree" :data="deptsList" :props="{label:'name'}" default-expand-all @node-click="nodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="employeeInfo.correctionTime" style="width:80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="closeDidlog">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import employmentConst from '@/api/constant'
import { getDepartmentList } from '@/api/department'
import { toTreeData } from '@/utils'
import { addEmployee } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isShowTree: false,
      deptsList: [],
      employmentConst,
      employeeInfo: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 2, max: 8, message: '长度为2-8', trigger: 'blur' }],
        mobile: [{ required: true, message: '该项不能为空', trigger: 'blur' },
          { pattern: /^1[356789][0-9]{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }],
        formOfEmploymet: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    const { depts } = await getDepartmentList()
    this.deptsList = toTreeData(depts, '')
  },
  methods: {
    // 树形控件点击事件，参数为当前节点数据
    nodeClick(data) {
      // console.log(data)
      this.employeeInfo.departmentName = data.name
      this.isShowTree = false
    },
    // 新增
    async btnOk() {
      // console.log(this.employeeInfo)
      // 校验
      await this.$refs.formData.validate()
      // 新增
      await addEmployee(this.employeeInfo)
      this.$message.success('新增成功')

      // 关闭弹窗
      this.$emit('closeDidlog')
      this.$parent.showDialog = false
      // 更新数据
      this.$parent.getEmployeeInfoList()
    },
    // 取消 关闭
    closeDidlog() {
      this.employeeInfo = {}
      this.$refs.formData.resetFields()
      this.$emit('closeDidlog')
    }
  }

}
</script>
<style lang="scss" scoped>
.deptsTree{
  position: absolute;
  width: 80%;
  height: 200px;
  overflow: auto;
  z-index: 2;
  border: 1px solid #666;
}
</style>
